// 函数重组的优点
// ❌ 联合类型的问题：输入明确，输出仍可能不匹配
enum MsgType { Mp3 = 0, Audio = 1 }
type MsgItem = { name: string; type: MsgType }
function searchMsg1(condition: MsgType | number): MsgItem | undefined | MsgItem[] {
  if (typeof condition === 'object') { return condition }
  else if (typeof condition === 'number') { return [] }
}
// ✅ 函数重载的解决：输入与输出严格对应
function searchMsg(input: MsgType): MsgType;
function searchMsg(input: number): MsgType[];
function searchMsg(input: any): any { /* ... */ }

// 重载签名与实现签名
enum MediaType { Mp4 = 'Mp4', Audio = 'Audio' }
type MediaItem = { mediaName: string; mediaType: MediaType; [key: string]: any }
let mediaList: MediaItem[] = [
  { id: 0, mediaName: "青衣.mp3", mediaType: MediaType.Audio },
  { id: 1, mediaName: "哪吒2.mp4", mediaType: MediaType.Mp4 },
  { id: 2, mediaName: "京城游.mp3", mediaType: MediaType.Audio },
]
// 用函数重载，实现：当用id查询时，返回一个对象。当用type查询时，返回一组对象
function seachMedia(id: number): MediaItem | undefined;  // 重载签名
function seachMedia(mediaType: MediaType): MediaItem[];
function seachMedia(condition: number | MediaType): undefined | MediaItem | MediaItem[] {  // 实现签名
  if (typeof condition === 'number') {
    return mediaList.find(item => item.id === condition)
  } else {
    return mediaList.filter(item => item.mediaType === condition)
  }
}
console.log("搜索id为1: ", seachMedia(1))
console.log("搜索音频类的: ", seachMedia(MediaType.Audio))


export {}